// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package user

import (
	"audio-server/internal/model"
	"audio-server/internal/svc"
	"audio-server/internal/types"
	"context"
	"database/sql"
	"encoding/json"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (resp *types.UserResp, err error) {
	userId, _ := l.ctx.Value("userId").(json.Number).Int64()
	usersModel := model.NewUsersModel(l.svcCtx.Conn)
	user, err := usersModel.FindById(l.ctx, userId)
	if err != nil || errors.Is(err, sql.ErrNoRows) {
		_ = errors.New("用户不存在")
	}

	return &types.UserResp{
		Avatar:     user.Avatar,
		CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
		Gender:     int(user.Gender),
		Nickname:   user.Nickname,
		RoleIds:    user.RoleIds,
		Username:   user.Username,
		Id:         user.Id,
		ApiKey:     user.ApiKey,
	}, nil
}
